<?php
/**
 * OpenidController.php
 *
 * @category   Madoqua
 * @package    Module
 * @subpackage Front
 */

/** abstract base controller */
require_once 'Madoqua/Controller/Action/Abstract.php';

/** OpenID consumer */
require_once "Zend/OpenId/Consumer.php";

/** OpenID storage */
require_once "Zend/OpenId/Consumer/Storage/File.php";

/**
 * Front_OpenidController
 *
 * OpenID controller
 *
 * @category   Madoqua
 * @package    Module
 * @subpackage Front
 */
class Front_OpenidController extends Madoqua_Controller_Action_Abstract {

    /**
     * openid consumer
     *
     * @var Zend_OpenId_Consumer
     */
    private $_consumer;

    /**
     * initialization
     */
    public function init() {
        parent::init();

        $dir = Madoqua_Application::getDataDirectory() . DIRECTORY_SEPARATOR  . 'openid/';
        $storage = new Zend_OpenId_Consumer_Storage_File($dir);
        
        $this->_consumer = new Zend_OpenId_Consumer($storage, true);
        //OpenID consumer
    }

    /**
     * try to figure out what to do with our poor user
     */
    public function indexAction() {
        if (($identifier = $this->_session->openid) !== null) {
            //we have an identifier in the session
            $this->_forward('success');
            return;
        } else {
            $this->_forward('identify');
        }
        
    }
    
    /**
     * identify with OpenID
     */
    public function identifyAction()
    {

        $form = $this->_formLoader->get();
        $this->view->form = $form;
        //the login form

        if ($this->getRequest()->isPost() && $form->isValid($this->getRequest()->getPost())) {
            //we have a post! \ó/

            if (!$this->_consumer->login($form->getValue('identifier'))) {
                $this->view->failed = true;
            }
            //FIXME integrate into form

        } elseif (($mode = $this->getRequest()->getQuery('openid_mode', false)) !== false) {
            //we have a succesful return \ó/

            $identifier = null;
            //the OpenID identifier, will be filled by verify()
            
            if ($this->_consumer->verify($this->getRequest()->getQuery(), $identifier)) {
                $this->_session->openid = $identifier;
                //remember the identifier in the session
                $this->getHelper('redirector')->gotoRoute(
                    array(
                        'controller' => 'openid',
                        'action' => 'success',
                        'module' => 'front'                    
                    )
                );
                //redirect back
            }

            $this->view->failed = true;
            //something went wrong, abort abort
        }

    }
    
    /**
     * wooh! it worked!
     */
    public function successAction()
    {
        $this->view->identifier = $this->_session->openid;
        //set it in the view
        
        $form = $this->_formLoader->get();
        $form->setRouteAction(array(
            'action' => 'success'
        ));
        $this->view->form = $form;
        //the logout form
        
        if ($this->getRequest()->isPost() && $form->isValid($this->getRequest()->getPost())) {
            //reset
            $this->getHelper('redirector')->gotoRoute(
                array(
                    'controller' => 'openid',
                    'action' => 'logout',
                    'module' => 'front'                    
                )
            );
            //redirect back
        }
    }

    /**
     * logout from openid
     */
    public function logoutAction() {
        unset($this->_session->openid);
    }
}